<?php
/**
 * 订单管理类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);

class order extends admin {

	private $times_db;
	
	function __construct() {
		parent::__construct();
		$this->db =  pc_base::load_model('order_model');
		$this->module_db = pc_base::load_model('module_model');
	}

	public function init() {
		$page = intval($_GET['page']);
		order_auto_done();//订单状态处理
		
		//搜索框
		$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
		$type = isset($_GET['type']) ? $_GET['type'] : '';
		$status = isset($_GET['status']) ? $_GET['status'] : '';
		$goodtype = isset($_GET['goodtype']) ? $_GET['goodtype'] : '';
		$start_time = isset($_GET['start_time']) ? $_GET['start_time'] : '';
		$end_time = isset($_GET['end_time']) ? $_GET['end_time'] : date('Y-m-d', SYS_TIME);
				
		if (isset($_GET['search'])) {
			
			//默认选取一个月内的用户，防止用户量过大给数据造成灾难
			$where_start_time = strtotime($start_time) ? strtotime($start_time) : 0;
			$where_end_time = strtotime($end_time) + 86400;
			//开始时间大于结束时间，置换变量
			if($where_start_time > $where_end_time) {
				$tmp = $where_start_time;
				$where_start_time = $where_end_time;
				$where_end_time = $tmp;
				$tmptime = $start_time;
				
				$start_time = $end_time;
				$end_time = $tmptime;
				unset($tmp, $tmptime);
			}
			
			
			$where = '';

			if($status=='') {
			}else{
				$where .= "`order_status` = '$status' AND ";
			}
			
			if($goodtype=='') {
			}else{
				$where .= "`goods` LIKE '%$goodtype%' AND ";
			}

			$where .= "`order_time` BETWEEN '$where_start_time' AND '$where_end_time' AND ";
		
			if($keyword) {
				if ($type == '1') {
					$where .= "`user_id` = '$keyword'";
				} elseif($type == '2') {
					$where .= "`shop_id` = '$keyword'";
				} elseif($type == '3') {
					$where .= "`name` like '%$keyword%'";
				} elseif($type == '4') {
					$where .= "`order_id` = '$keyword'";
				} elseif($type == '5') {
					$where .= "`goods` LIKE '%$keyword%'";
				}else{
					$where .= "`user_id` = '$keyword'";
				}
			} else {
				$where .= '1';
			}
			
		} else {
			$where = '';
		}
		
		$infos = $this->db->listinfo($where,'order_time desc',$_GET['page'],'10');
		$pages = $this->db->pages;
		
		
		if($where==''){
			$ordertotal = $this->db->query("SELECT SUM(price) FROM v9_order");
			$ordertotal  = mysql_fetch_assoc($ordertotal);
		}else{
			$ordertotal = $this->db->query("SELECT SUM(price) FROM v9_order WHERE ".$where);
			$ordertotal  = mysql_fetch_assoc($ordertotal);
		}

		include $this->admin_tpl('order_list');
	}

	public function del() {
		$ids = $_REQUEST['orderid'];
		if (is_array($ids)) {
			foreach ($ids as $id) {
				$this->db->delete(array('order_id'=>$id));
			}
		} else {
			showmessage(L('illegal_operation'));
		}
		showmessage(L('operation_success'), HTTP_REFERER);
	}

	public function status() {
		$ids = $_REQUEST['orderid'];
		$status = $_REQUEST['status'];
		if (is_array($ids)) {
			foreach ($ids as $id) {
				
				$this->db->update(array('order_status'=>$status),array('order_id'=>$id));
				if($status=='-1'){
					//商品库存销量处理
					$orderdata = $this->db->get_one(array('order_id'=>$id));
					$gooddata=string2array($orderdata['goods']);
					if($gooddata[$orderdata[frist_good]]['goodtype']=='goods'){
						$con_db = pc_base::load_model('content_model');
						$con_db->set_model(14);
						foreach($gooddata as $val){
							$con_db->update(array('stock'=>$val['stock']+$val['num'],'hot'=>-$val['num']),array('id'=>$val['id']));
						} 
					}
				}
				
				
				
			}
		} else {
			showmessage(L('illegal_operation'));
		}
		showmessage(L('operation_success'), HTTP_REFERER);
	}
	
}
?>